package com.example.order.config;

import com.example.order.web.RestTemplateTraceIdInterceptor;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;
import java.util.Arrays;


@Component
public class RestTemplateConfig {
    @Bean
    @LoadBalanced
    // 没有@LoadBalanced报错java.net.UnknownHostException: payment-service
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        RestTemplate restTemplate = builder
                .setConnectTimeout(Duration.ofSeconds(10))
                .setReadTimeout(Duration.ofMinutes(1))
                .build();
        // 为RestTemplate添加拦截器
        restTemplate.setInterceptors(Arrays.asList(new RestTemplateTraceIdInterceptor()));
        return restTemplate;
    }


}
